package org.martyn.danbodroid.gui.activitys;

import java.util.ArrayList;

import org.martyn.danbodroid.DanboDroid;
import org.martyn.danbodroid.R;

import org.martyn.danbodroid.database.ServersTable;
import org.martyn.danbodroid.gui.ActionBar;
import org.martyn.danbodroid.preferences.Preferences;
import org.martyn.danbooru.DanbooruConnectSettings;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class HomeActivity extends BaseActivity
{
	private static final int DIALOG_SERVERS = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.home_activity);
		
		ActionBar actionBar = this.setupActionBar();
		
		actionBar.setTitle(this.getTitle());
		actionBar.setSearchButtonVisable(true);
		
		this.findViewById(R.id.home_btn_posts).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(getBaseContext(), PostsListActivity.class);
				startActivity(intent);
			}
		});
		
		this.findViewById(R.id.home_btn_tags).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				
			}
		});
		
		this.findViewById(R.id.home_btn_comments).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
			
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		boolean result = super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.home_activity_menu, menu);
		return result;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		// Handle item selection
		switch (item.getItemId())
		{
			case R.id.menuItemSettings:
			{
				this.startActivityForResult(new Intent(this,
						MainPreferencesActivity.class), 0);

				break;
			}
			case R.id.menuItemSelectDanbooru:
			{
				showDialog(DIALOG_SERVERS);
				
				break;
			}
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		if(id == DIALOG_SERVERS)
		{
			int preSelection = Preferences.loadSelectedServerSettings(this);
			
			DanboDroid danboDroid = (DanboDroid) this.getApplication();
			
			final ArrayList<DanbooruConnectSettings> danbooruConnectSettingsList = ServersTable.getAll(danboDroid.getDatabaseHelper());
			
			ArrayList<String> serverNamesList = new ArrayList<String>();
			ArrayList<Integer> serverIdList = new ArrayList<Integer>();
			
			if(danbooruConnectSettingsList.size() != 0) {
				
				for(int i = 0; i < danbooruConnectSettingsList.size(); i++)
				{
					serverIdList.add(danbooruConnectSettingsList.get(i).id);
					serverNamesList.add(danbooruConnectSettingsList.get(i).name);	
				}
				
				final Integer[] serverIds = serverIdList.toArray(new Integer[serverIdList.size()]);
				final String[] serverNames = serverNamesList.toArray(new String[serverNamesList.size()]);
				
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				
				builder.setTitle("Select Danbooru Server");

				builder.setSingleChoiceItems(
						(CharSequence[])serverNames,
						preSelection,
						new DialogInterface.OnClickListener() {
						    public void onClick(DialogInterface dialog, int item) {
				
						    	//Store this new 'last used server'
								Preferences.storeSelectedServerSettings(getApplicationContext(), serverIds[item].intValue());
								
								dismissDialog(DIALOG_SERVERS);
							}
						});
		
				return builder.create();
			}
			else
			{
				Toast.makeText(this, R.string.toast_noserversfound, Toast.LENGTH_SHORT).show();				
			}
			
		}
		
		return super.onCreateDialog(id);
	}
	
}














